/*
 * @Author: Jason Liu
 * @Date: 2025-06-05 13:19:40
 * @Desc: 
 */

const $modelService = require("w-vue-chongqing/src/api/dataModel");
const sqlScript = require("./sqlScript").default;
const { defaultFieldsList } = require("../../indicatorNavigation/config");

export default {
  components: {sqlScript},
  props: {
    value: {
      type: Array,
      default: () => {
        return [];
      }
    },
    editEnabled: {
      type:Boolean,
      default: true // 默认可编辑
    },
    id: { // 表id
      type: String,
      default: ""
    }
  },
  data() {
    return {
      loading: false,
      visible: this.show,
      fields: this.value,
      activeKey: "hdwFile",
      databaseOptions: [],
      sqlVisible: false,
      isAddDefault: false,
    };
  },
  created() {
    this.getDataBase();
  },
  methods: {
    /**
     * @Author: Jason Liu
     * @description: 查询
     */
    onSearch(e) {},
    /**
     * @Author: Jason Liu
     * @description: 添加字段
     */
    addField() {
      if((!this.isAddDefault || this.fields.length == 0) && !this.id && this.editEnabled) {
        let fields = defaultFieldsList.map(item => { 
          return item.columnCode;
        });
        this.fields = this.fields.filter(item => {
          return fields.indexOf(item.columnCode) == -1;
        });
        this.fields = this.fields.concat(defaultFieldsList);
        this.isAddDefault = true;
      }
      
      const field = {
        columnCode: undefined,
        columnSize: 64,
        columnType: "varchar",
        defaultValue: "",
        columnName: undefined,
        isNull: false,
        pk: false,
        columnAccuracy: undefined
      };
      // this.fields.push(field);
      this.fields.splice(this.fields.length-4, 0, field);

      this.$nextTick(() => {
        setTimeout(() => {
          this.$refs.field_table.scrollToRow(field);
        }, 0);
      });
    },
    // 删除字段
    deleteRow(row) {
      this.fields = this.fields.filter(item => {
        return row._X_ROW_KEY != item._X_ROW_KEY;
      });
      this.changeNodel();
    },
    defaultFieldDisabled(row) {
      let val = defaultFieldsList.find(item => {
        return item.columnCode == row.columnCode;
      });
      return !!val;
    },
    // 获取数据库类型
    async getDataBase() {
      this.databaseOptions = [];
      const { data } = await $modelService.getDataBaseDataType();
      this.databaseOptions = data || [];
    },
    keyCode(row) {
      let keyValue = "";
      let val = row.columnCode.replace(/[\u4e00-\u9fff\u3400-\u4DBf]/g, '');
      for (let i = 0; i < val.length; i++) {
        if (val.charCodeAt(i) > 0 && val.charCodeAt(i) < 255) {
          keyValue += val[i];
        }
      }
      row.columnCode = keyValue;
      this.changeNodel();
    },
    /**
     * @Author: Jason Liu
     * @description: 修改数据
     */
    changeNodel() {
      this.$emit("input", this.fields);
      this.$emit("change", this.fields);
    }
  },
  watch: {
    value(val) {
      this.fields = val;
    },
    fields(val) {
      this.$emit("input", val);
    }
  }
};
